package br.jonasLab.financistoweb.control;

import java.util.HashMap;
import java.util.List;

import br.jonasLab.financistoweb.persistence.BalancoMesAnoDAO;
//import br.jonasLab.financistoweb.persistence.LegadoDAO;
import br.jonasLab.financistoweb.vo.BalancoMesAnoVO;
import br.jonasLab.financistoweb.vo.DetalheBalacoVO;
import br.jonasLab.financistoweb.vo.SaldoFimMesVO;


public class BalancoControl implements IBalancoControl {

	/*
	@Override
	public List<BalancoMesAnoVO> balancoMesAno() throws Exception {
		Double saldoInicial = new Double(0);
		Double saldoFinal = new Double(0);
		LegadoDAO daoLegado = new LegadoDAO();
		BalancoMesAnoDAO dao = new BalancoMesAnoDAO();
		List<BalancoMesAnoVO> listRetorno = dao.balancoMesAno();
		HashMap<String, Double> listLegado = daoLegado.legadoMes(); 
		if (listRetorno != null){
			for (int i = 0; i < listRetorno.size(); i++) {
				Double vrlLegado = listLegado.get(listRetorno.get(i).getMesAno());
				if(vrlLegado!=null){
					saldoInicial+= vrlLegado;
					saldoFinal+= vrlLegado;
				}
				calcularValores(listRetorno.get(i),saldoInicial,saldoFinal);
				saldoInicial = listRetorno.get(i).getSaldoInicial();
				saldoFinal = listRetorno.get(i).getSaldoFinal();
			}
		}
		return listRetorno;
	}*/
	/*
	private BalancoMesAnoVO calcularValores(BalancoMesAnoVO vo , Double saldoInicial , Double saldoFinal){
		saldoInicial = saldoFinal;
		saldoFinal+= vo.getGanho()-vo.getRetido()+vo.getPagCartao()+vo.getGasto();
		vo.setTotalGastos(vo.getRetido()+vo.getPagCartao()+vo.getGasto());
		vo.setSaldoInicial(saldoInicial);
		vo.setSaldoFinal(saldoFinal);
		return vo;
	}*/
	private final String IGUAL = "=";
	private final String IN = "IN";
	private final String NOT_IN = "NOT IN";
	
	@Override
	public List<BalancoMesAnoVO> balancoMesAno() throws Exception {
		BalancoMesAnoDAO dao = new BalancoMesAnoDAO();
		List<BalancoMesAnoVO> listRetorno = dao.balancoMesAno();
		List<SaldoFimMesVO> listSaldoFimMes = dao.saldoFimMes();
		if (listRetorno != null && listSaldoFimMes != null){
			HashMap<String, SaldoFimMesVO> listFimMes = calcularValoresFimMes(listSaldoFimMes);
			for (int i = 0; i < listRetorno.size(); i++) {
				listRetorno.get(i).setSaldoInicial(listFimMes.get(listRetorno.get(i).getMesAno()).getSaldoInicial());
				listRetorno.get(i).setSaldoFinal(listFimMes.get(listRetorno.get(i).getMesAno()).getSaldoFinal());
			}
		}
		return listRetorno;
	}
	
	private HashMap<String, SaldoFimMesVO> calcularValoresFimMes(List<SaldoFimMesVO> list){
		Double saldoInicial = new Double(0);
		Double saldoFinal = new Double(0);
		HashMap<String, SaldoFimMesVO> ret = new HashMap<String, SaldoFimMesVO>(); 
		for (int i = 0; i < list.size(); i++) {
			saldoInicial = saldoFinal;
			saldoFinal+= list.get(i).getSantander()+list.get(i).getMaos()+list.get(i).getBancoBrasil();
			list.get(i).setSaldoInicial(saldoInicial);
			list.get(i).setSaldoFinal(saldoFinal);
			ret.put(list.get(i).getMesAno(), list.get(i));
		}
		return ret;
	}

	@Override
	public List<DetalheBalacoVO> listDetalheGanhos(String mesAno) throws Exception {
		String categories = "25,41,121,112,124,36";
		String toAccount = "0,1,2,12";
		BalancoMesAnoDAO dao = new BalancoMesAnoDAO(); 
		List<DetalheBalacoVO> listVOs = dao.listDetalheBalanco(mesAno, IN, categories, IN, toAccount, "", "");
		return listVOs;
	}

	@Override
	public List<DetalheBalacoVO> listDetalheRetido(String mesAno)
			throws Exception {
		String categories = "3,119";
		BalancoMesAnoDAO dao = new BalancoMesAnoDAO();
		List<DetalheBalacoVO> listVOs = dao.listDetalheBalanco(mesAno, IN, categories, "", "", "","");
		return listVOs;
	}

	@Override
	public List<DetalheBalacoVO> listDetalhePagCartao(String mesAno)
			throws Exception {
		String categories = "18";
		BalancoMesAnoDAO dao = new BalancoMesAnoDAO();
		List<DetalheBalacoVO> listVOs = dao.listDetalheBalanco(mesAno, IGUAL, categories, "","","","");
		return listVOs;
	}

	@Override
	public List<DetalheBalacoVO> listDetalheGasto(String mesAno)
			throws Exception {
		String categories = "25,41,121,112,124,2,3,18,32,42.119,126,37,36,42,31,118,-1,30,28";
		String toAccount="0,11";
		String fromAccount="1,2,12,14";
		BalancoMesAnoDAO dao = new BalancoMesAnoDAO();
		List<DetalheBalacoVO> listVOs = dao.listDetalheBalanco(mesAno, NOT_IN, categories, IN, toAccount, IN, fromAccount);
		return listVOs;
	}

	@Override
	public List<DetalheBalacoVO> listRankingCategoriaPorMes(String mesAno)
			throws Exception {
		String categories = "25,41,121,112,124,2,3,18,32,42.119,126,37,36,42,31,118,-1,30,28";
		String toAccount="0,11";
		String fromAccount="1,2,12,14";
		BalancoMesAnoDAO dao = new BalancoMesAnoDAO();
		List<DetalheBalacoVO> listVOs = dao.listRankingCategoriaPorMes(mesAno, NOT_IN, categories, IN, toAccount, IN, fromAccount);
		return listVOs;
	}
}